package cosplay.DAO;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.CallableStatementCallback;
import org.springframework.jdbc.core.CallableStatementCreator;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import cosplay.Beans.Comments;

@Repository (value="commentdao")
public class CommentDAOImpl implements CommentDAO{
	@Autowired
	private JdbcTemplate jt;

	@Override
	public int add(final Comments comment) {
		// TODO Auto-generated method stub
		int id=0;
		if(comment!=null)
		{
			Object[] obj=(Object[]) jt.execute(
					new CallableStatementCreator(){

						@Override
						public CallableStatement createCallableStatement(
								Connection conn) throws SQLException {
							// TODO Auto-generated method stub
							String sql="CALL COMMENT_ADD(?,?,?,?,?);";
							CallableStatement stm=conn.prepareCall(sql);
							stm.setInt(1, comment.getUserID());
							stm.setInt(2, comment.getPhotoID());
							stm.setString(3, comment.getTitle());
							stm.setString(4, comment.getContent());
							stm.registerOutParameter(5, java.sql.Types.INTEGER);
							return stm;
						}}, new CallableStatementCallback(){

						@Override
						public Object doInCallableStatement(
								CallableStatement stm) throws SQLException,
								DataAccessException {
							// TODO Auto-generated method stub
							ResultSet rs=stm.executeQuery();
							Object[] obj=new Object[2];
							obj[0]=stm.getInt(5);
							obj[1]=rs;
							return obj;
						}});
			id=((Integer) obj[0]).intValue();
		}
		return id;
	}

	@Override
	public boolean delete(final int cid) {
		// TODO Auto-generated method stub
		boolean sucess=false;
		if(cid>0)
		{
			jt.execute(
					new CallableStatementCreator(){

						@Override
						public CallableStatement createCallableStatement(
								Connection conn) throws SQLException {
							// TODO Auto-generated method stub
							String sql="CALL COMPITITION_DELETE(?);";
							CallableStatement stm=conn.prepareCall(sql);
							stm.setInt(1, cid);
							return stm;
						}}, new CallableStatementCallback(){

						@Override
						public Object doInCallableStatement(
								CallableStatement stm) throws SQLException,
								DataAccessException {
							// TODO Auto-generated method stub
							ResultSet rs=stm.executeQuery();
							Object[] obj=new Object[2];
							obj[0]=null;
							obj[1]=rs;
							return obj;
						}});
			sucess=true;
		}
		return sucess;
	}

	@Override
	public boolean edit(final Comments comment, final int id) {
		// TODO Auto-generated method stub
		if (id<0)
			return false;
		boolean sucess=false;
		if(comment!=null)
		{
			jt.execute(
					new CallableStatementCreator(){

						@Override
						public CallableStatement createCallableStatement(
								Connection conn) throws SQLException {
							// TODO Auto-generated method stub
							String sql="CALL COMPETITION_EDIT(?,?,?);";
							CallableStatement stm=conn.prepareCall(sql);
							stm.setInt(1, id);
							stm.setString(2, comment.getTitle());
							stm.setString(3, comment.getContent());
							return stm;
						}}, new CallableStatementCallback(){

						@Override
						public Object doInCallableStatement(
								CallableStatement stm) throws SQLException,
								DataAccessException {
							// TODO Auto-generated method stub
							ResultSet rs=stm.executeQuery();
							Object[] obj=new Object[2];
							obj[0]=null;
							obj[1]=rs;
							return obj;
						}});
			sucess=true;
		}
		return sucess;
	}

	@Override
	public boolean approve(final int cid) {
		// TODO Auto-generated method stub
		if(cid>0)
		{
			jt.execute(
					new CallableStatementCreator(){

						@Override
						public CallableStatement createCallableStatement(
								Connection conn) throws SQLException {
							// TODO Auto-generated method stub
							String sql="CALL COMMENT_APPROVE(?);";
							CallableStatement stm=conn.prepareCall(sql);
							stm.setInt(1, cid);
							return stm;
						}}, new CallableStatementCallback(){

						@Override
						public Object doInCallableStatement(
								CallableStatement stm) throws SQLException,
								DataAccessException {
							// TODO Auto-generated method stub
							ResultSet rs=stm.executeQuery();
							Object[] obj=new Object[2];
							obj[0]=null;
							obj[1]=rs;
							return obj;
						}});
				return true;
		}
		return false;
	}

	@Override
	public boolean disapprove(final int cid) {
		// TODO Auto-generated method stub
		if(cid>0)
		{
			jt.execute(
					new CallableStatementCreator(){

						@Override
						public CallableStatement createCallableStatement(
								Connection conn) throws SQLException {
							// TODO Auto-generated method stub
							String sql="CALL COMMENT_DISAPPROVE(?);";
							CallableStatement stm=conn.prepareCall(sql);
							stm.setInt(1, cid);
							return stm;
						}}, new CallableStatementCallback(){

						@Override
						public Object doInCallableStatement(
								CallableStatement stm) throws SQLException,
								DataAccessException {
							// TODO Auto-generated method stub
							ResultSet rs=stm.executeQuery();
							Object[] obj=new Object[2];
							obj[0]=null;
							obj[1]=rs;
							return obj;
						}});
				return true;
		}
		return false;
	}

	@Override
	public Comments get(final int cid) {
		// TODO Auto-generated method stub
		if(cid>0)
		{
			Object[] obj=(Object[]) jt.execute(
					new CallableStatementCreator(){

						@Override
						public CallableStatement createCallableStatement(
								Connection conn) throws SQLException {
							// TODO Auto-generated method stub
							String sql="CALL COMMENT_GET(?);";
							CallableStatement stm=conn.prepareCall(sql);
							stm.setInt(1, cid);
							return stm;
						}}, new CallableStatementCallback(){

						@Override
						public Object doInCallableStatement(
								CallableStatement stm) throws SQLException,
								DataAccessException {
							// TODO Auto-generated method stub
							ResultSet rs=stm.executeQuery();
							Object[] obj=new Object[2];
							obj[0]=null;
							obj[1]=null;
							if (rs.next())
							{
								Comments comment=new Comments();
								comment.setApproved(rs.getBoolean("approved"));
								comment.setContent(rs.getString("content"));
								comment.setEdited(rs.getTimestamp("edited"));
								comment.setPhotoID(rs.getInt("photoid"));
								comment.setPosted(rs.getTimestamp("posted"));
								comment.setTitle(rs.getString("title"));
								comment.setUserID(rs.getInt("userid"));
								obj[1]=comment;
							}
							return obj;
						}});
			if(obj[1]!=null)
				return (Comments) obj[1];
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Integer> getAllUnapproved() {
		// TODO Auto-generated method stub
		Object[] obj=(Object[]) jt.execute(
				new CallableStatementCreator(){

					@Override
					public CallableStatement createCallableStatement(
							Connection conn) throws SQLException {
						// TODO Auto-generated method stub
						String sql="CALL COMMENT_GETALLUNPROVED();";
						CallableStatement stm=conn.prepareCall(sql);
						return stm;
					}}, new CallableStatementCallback(){

						@Override
						public Object doInCallableStatement(
								CallableStatement stm) throws SQLException,
								DataAccessException {
							// TODO Auto-generated method stub
							ResultSet rs=stm.executeQuery();
							Object[] obj=new Object[2];
							obj[0]=null;
							obj[1]=null;
							List<Integer> competitions = new ArrayList<Integer>();
							while (rs.next())
							{
								competitions.add(rs.getInt("id"));
							}
							if (competitions.size()>0)
								obj[1]=competitions;
							return obj;
						}});
		if(obj[1]!=null)
			return (List<Integer>) obj[1];
		return null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Integer> getByPhoto(final int pid) {
		// TODO Auto-generated method stub
		if (pid>0)
		{
			Object[] obj=(Object[]) jt.execute(
					new CallableStatementCreator(){

						@Override
						public CallableStatement createCallableStatement(
								Connection conn) throws SQLException {
							// TODO Auto-generated method stub
							String sql="CALL COMMENT_GETBUPHOTO(?);";
							CallableStatement stm=conn.prepareCall(sql);
							stm.setInt(1, pid);
							return stm;
						}}, new CallableStatementCallback(){

							@Override
							public Object doInCallableStatement(
									CallableStatement stm) throws SQLException,
									DataAccessException {
								// TODO Auto-generated method stub
								ResultSet rs=stm.executeQuery();
								Object[] obj=new Object[2];
								obj[0]=null;
								obj[1]=null;
								List<Integer> competitions = new ArrayList<Integer>();
								while (rs.next())
								{
									competitions.add(rs.getInt("id"));
								}
								if (competitions.size()>0)
									obj[1]=competitions;
								return obj;
							}});
			if(obj[1]!=null)
				return (List<Integer>) obj[1];
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Integer> getByUser(final int uid) {
		// TODO Auto-generated method stub
		if (uid>0)
		{
			Object[] obj=(Object[]) jt.execute(
					new CallableStatementCreator(){

						@Override
						public CallableStatement createCallableStatement(
								Connection conn) throws SQLException {
							// TODO Auto-generated method stub
							String sql="CALL COMMENT_GETBUPHOTO(?);";
							CallableStatement stm=conn.prepareCall(sql);
							stm.setInt(1, uid);
							return stm;
						}}, new CallableStatementCallback(){

							@Override
							public Object doInCallableStatement(
									CallableStatement stm) throws SQLException,
									DataAccessException {
								// TODO Auto-generated method stub
								ResultSet rs=stm.executeQuery();
								Object[] obj=new Object[2];
								obj[0]=null;
								obj[1]=null;
								List<Integer> competitions = new ArrayList<Integer>();
								while (rs.next())
								{
									competitions.add(rs.getInt("id"));
								}
								if (competitions.size()>0)
									obj[1]=competitions;
								return obj;
							}});
			if(obj[1]!=null)
				return (List<Integer>) obj[1];
		}
		return null;
	}

}
